package cn.xkai.exercise.c;

import java.util.HashMap;

/**
 * @description: 无重复字符的最长子串
 * @author: kaixiang
 * @date: 2022-07-28
 **/
public class Solution105 {
    public int lengthOfLongestSubstring(String s) {
        int max = 0, start = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                start = Math.max(start, map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i), i);
            max = Math.max(max, i - start + 1);
        }
        return max;
    }

    public static void main(String[] args) {
        String s = "abba";
        Solution105 solution105 = new Solution105();
        System.out.println(solution105.lengthOfLongestSubstring(s));
    }
}
